package cn.edu.glut.user.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.glut.dto.Response;
import cn.edu.glut.dto.TextResponse;
import cn.edu.glut.dto.UserIntro;
import cn.edu.glut.entity.User;
import cn.edu.glut.user.service.UserDetailService;
import cn.edu.glut.utils.UploadUtils;

/**
 * 
 * @author Caizhfy
 * @email caizhfy@163.com
 * @createTime 2017年12月7日
 * @description
 *
 */
@RestController
@RequestMapping("/usr")
public class UserController {
	@Autowired
	private UserDetailService userDetailService;
	@Value("${file.path}")
	private String filePath;
	
	
	//登录
	@RequestMapping("/signin")
	public Response login(@RequestParam("type") int userType, @RequestParam("username") String username,
			@RequestParam("password") String password, HttpServletRequest req) {
		User user = userDetailService.toLogin(username, password, userType);
		HttpSession session = req.getSession();
		if(!StringUtils.isEmpty(user)){
			session.setAttribute("user", new UserIntro(user.getId(), userType));
			user.setPassword("");
			return Response.success(user);
		}
		return Response.success();
	}
	
	//注销
	@RequestMapping("/signout")
	public Response logout(HttpServletRequest req){
		HttpSession session = req.getSession();
		Enumeration<String> em = session.getAttributeNames();
		while(em.hasMoreElements()){
			session.removeAttribute(em.nextElement());
		}
		session.invalidate();
		return Response.success();
	}
	//富文本上传
	@RequestMapping("/upload")
	public TextResponse upload(@RequestParam("imgFile") MultipartFile imgFile, @RequestParam("dir") String dir, HttpServletRequest req){
		try {
			String fileSavePath = this.getClass().getResource(filePath).getPath();
			//获取文件名称
			String localFileName = UploadUtils.MvcUpload(imgFile,fileSavePath);
			//得到本地访问路径
			String fileUrl = req.getServletContext().getContextPath() + filePath + "/" + localFileName;
			return TextResponse.success(fileUrl);
		} catch (Exception e) {
			e.printStackTrace();
			return TextResponse.failure("文件不能为空或者数据格式出错!");
		}
	}
	
	//修改密码
	@RequestMapping("/update/pwd")
	public Response changePassword(HttpServletRequest req, @RequestParam("oldPwd")String oldPwd, @RequestParam("newPwd") String newPwd){
		HttpSession session = req.getSession();
		UserIntro user = (UserIntro) session.getAttribute("user");
		return Response.success(userDetailService.changePassword(user, oldPwd, newPwd));
	}
}
